﻿<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="排序" prop="Sort">
          <a-input v-model="entity.Sort" autocomplete="off" placeholder="越小越靠前" />
        </a-form-model-item>
        <a-form-model-item :label="this.parentObj.hostTitle" prop="HostId">
          <a-select
            v-model="entity.HostId"
            allowClear
            :dropdown-style="{ overflow: 'auto' }"
            :getPopupContainer="triggerNode => { return triggerNode.parentNode || document.body;}"
          >
            <a-select-option v-for="item in this.hostTableData" :key="item.Id" :value="item.Id">
              {{ item.Value }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item :label="this.parentObj.slaveTitle" prop="SlaveId">
          <a-select
            v-model="entity.SlaveId"
            allowClear
            :dropdown-style="{ overflow: 'auto' }"
            :getPopupContainer="triggerNode => { return triggerNode.parentNode || document.body;}"
          >
            <a-select-option v-for="item in this.slaveTableData" :key="item.Id" :value="item.Id">
              {{ item.Name }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="自定义编码" prop="Code">
          <a-input v-model="entity.Code" autocomplete="off" placeholder="自定义编码" />
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Remark">
          <a-input v-model="entity.Remark" autocomplete="off" placeholder="备注" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: '',
      hostTableName: '', // 主表名称
      hostTitle: '', // 主表标题
      hostTableData: [], // 主表数据源
      slaveTableName: '', // 从表名称
      slaveTitle: '', // 从表标题
      slaveTableData: [] // 从表数据源
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm (id, title) {
      this.init()
      this.title = title
      if (id) {
        this.loading = true
        this.$http.post('/Base_BasicData/Base_GeneralConfig/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
      }
      this.initHostTableData()
      this.initSlaveTableData()
    },
    initHostTableData () {
      this.entity.HostTableName = this.parentObj.hostTableName
      this.loading = true
      this.$http.post('/Base_BasicData/Base_GeneralConfig/GetDataListByTableName', { TableName: this.entity.HostTableName }).then(resJson => {
        this.hostTableData = resJson.Data
      })
      this.loading = false
    },
    initSlaveTableData () {
      this.entity.SlaveTableName = this.parentObj.slaveTableName
      this.loading = true
      this.$http.post('/Base_BasicData/Base_GeneralConfig/GetDataListByTableName', { TableName: this.entity.SlaveTableName }).then(resJson => {
        this.slaveTableData = resJson.Data
      })
      this.loading = false
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/Base_BasicData/Base_GeneralConfig/SaveData', this.entity).then(resJson => {
          this.loading = false
          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false
            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
<style scoped lang="less">
/deep/.ant-select-dropdown-menu{
  max-height: 180px
}
</style>
